package com.example.itshome.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.entity.CommonEntity;

import java.util.List;

public class CommonRecycleViewAdapter extends RecyclerView.Adapter<CommonRecycleViewAdapter.CommonViewHolder> {

    private final List<CommonEntity> commonEntities;
    private final Context context;
    public CommonRecycleViewAdapter(Context context, List<CommonEntity> entities){
        commonEntities=entities;
        this.context=context;
    }

    @NonNull
    @Override
    public CommonViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=View.inflate(context,R.layout.recycle_common_list_item,null);
        CommonViewHolder holder=new CommonViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull CommonViewHolder holder, int position) {
        CommonEntity entity=commonEntities.get(position);
        holder.nicknameTextView.setText(entity.getNickname());
        holder.messageTextView.setText(entity.getMessage());
        holder.timeTextView.setText(entity.getTime());
        Glide.with(context).load(entity.getHeader()).placeholder(R.drawable.my_page_header_example).circleCrop().into(holder.headerImageView);
    }

    @Override
    public int getItemCount() {
        return commonEntities.size();
    }

    static class CommonViewHolder extends RecyclerView.ViewHolder {

        ImageView headerImageView;
        TextView nicknameTextView;
        TextView messageTextView;
        TextView timeTextView;
        public CommonViewHolder(@NonNull View itemView) {
            super(itemView);
            headerImageView=itemView.findViewById(R.id.imv_recycle_common_list_header);
            nicknameTextView=itemView.findViewById(R.id.txv_recycle_common_list_sender_name);
            messageTextView=itemView.findViewById(R.id.txv_recycle_common_list_message);
            timeTextView=itemView.findViewById(R.id.txv_recycle_common_list_time);
        }
    }

    public void addItem(CommonEntity entity){
        commonEntities.add(0,entity);
    }

}
